///////////////////////////////////////////////////////////////
// File: "COptionsState.cpp"
// Author: Keith Maggio (KM)
// Purpose: The options for the game get modified here.
///////////////////////////////////////////////////////////////

#include "COptionsState.h"
#include "CWiimote.h"
#include "../Wrappers/CSGD_DirectInput.h"
#include "CBitmapFontEngine.h"
#include "CMusicManager.h"
#include "CSoundManager.h"
#include "CTransitionScreen.h"
#include "CMainMenuState.h"
#include "..//Wrappers/CSGD_TextureManager.h"
#include "CGame.h"

COptionsState::COptionsState(void)
{
	m_bPause = false;
	m_nDifficulty = NORMAL;
	m_bExitingToMain = false;
	m_nFontID = -1;
	m_nMusicButtons = -1;
	m_fVolPanTimer = 0;
	SetFromGame(false);
	SetExitToGame(false);

}

COptionsState::~COptionsState(void)
{
}

void COptionsState::Enter()
{
	//First selected object when you enter the state
	m_nSelection = 0;
	
	m_bPause = false;

	m_cScreen.LoadScreen("Screens/BS_OptionsScreen.xml");

	m_nFontID = CBitmapFontEngine::GetInstance()->LoadFont("BS_TechDemoTitle.xml");
	m_nMusicButtons = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/BS_MusicButtons.png");
	m_bExitingToMain = false;

	m_bIsSubMenu = false;
	SetExitToGame(false);

	m_fVolPanTimer = 0;
	

	
}

void COptionsState::Exit()
{
	
}

bool COptionsState::Input(float fElapsedTime)
{
	CWiimote* pWii = CWiimote::GetInstance();
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();

	if (pWii->IsButtonJustPressed(WII_UP) || pDI->KeyPressed(DIK_UP))
	{
		if (!m_bIsSubMenu)
		{
			--m_nSelection; 
		}
		else
		{
			switch(m_nSubSelection)
			{
			case MUSIC_PLAY:
				m_nSubSelection = MUSIC_VOL;
				break;
			case MUSIC_PREV:
				m_nSubSelection = MUSIC_VOL;
				break;
			case MUSIC_NEXT:
				m_nSubSelection = MUSIC_VOL;
				break;
			case SOUND_PAN:
				m_nSubSelection = SOUND_VOL;
				break;
			}
		}
	}
	else if (pWii->IsButtonJustPressed(WII_DOWN) || pDI->KeyPressed(DIK_DOWN))
	{
		if (!m_bIsSubMenu)
		{
			++m_nSelection; 
		}
		else
		{
			switch(m_nSubSelection)
			{
			case MUSIC_VOL:
				m_nSubSelection = MUSIC_PLAY;
				break;
			case SOUND_VOL:
				m_nSubSelection = SOUND_PAN;
				break;
			}
		}
	}

	if (m_bIsSubMenu)
	{
		if ((pWii->IsButtonPressed(WII_LEFT) || pDI->KeyDown(DIK_LEFT))&& (m_nSubSelection ==  MUSIC_VOL||m_nSubSelection ==SOUND_VOL  || m_nSubSelection == SOUND_PAN) && m_fVolPanTimer >= 0.05f )
		{
			m_fVolPanTimer = 0;
			switch(m_nSubSelection)
			{
			case MUSIC_VOL:
				//Music Vol
				CMusicManager::GetInstance()->SetVol(CMusicManager::GetInstance()->GetVol() - 0.02f);
				break;
			case SOUND_VOL:
				//Sound Vol
				CSoundManager::GetInstance()->SetVolume(CSoundManager::GetInstance()->GetVol() - 2);
				//Preview the sound
				CSoundManager::GetInstance()->PlaySoundIndxBuffered(SOUND_BLOBBY_DEATH);
				break;
			case SOUND_PAN:
				//Sound Pan
				CSoundManager::GetInstance()->SetPan(CSoundManager::GetInstance()->GetPan() - 2);
				//Preview the sound
				CSoundManager::GetInstance()->PlaySoundIndxBuffered(SOUND_BLOBBY_DEATH);
				break;
			}
		}
		if ((pWii->IsButtonPressed(WII_RIGHT) || pDI->KeyDown(DIK_RIGHT))&& (m_nSubSelection ==  MUSIC_VOL||m_nSubSelection ==SOUND_VOL  ||m_nSubSelection == SOUND_PAN)  && m_fVolPanTimer >= 0.05f)
		{
			m_fVolPanTimer =0;
			switch(m_nSubSelection)
			{
			case MUSIC_VOL:
				//Music Vol
				CMusicManager::GetInstance()->SetVol(CMusicManager::GetInstance()->GetVol() + 0.02f);
				break;
			case SOUND_VOL:
				//Sound Vol
				CSoundManager::GetInstance()->SetVolume(CSoundManager::GetInstance()->GetVol() + 2);
				//Preview the sound
				CSoundManager::GetInstance()->PlaySoundIndxBuffered(SOUND_BLOBBY_DEATH);
				break;
			case SOUND_PAN:
				//Sound Pan
				CSoundManager::GetInstance()->SetPan(CSoundManager::GetInstance()->GetPan() + 2);
				//Preview the sound
				CSoundManager::GetInstance()->PlaySoundIndxBuffered(SOUND_BLOBBY_DEATH);
				break;
			}
		}


		if (pWii->IsButtonJustPressed(WII_LEFT) || pDI->KeyPressed(DIK_LEFT))
		{

			switch(m_nSubSelection)
			{

			case MUSIC_NEXT:
				m_nSubSelection = MUSIC_PLAY;
				break;
			case MUSIC_PLAY:
				m_nSubSelection = MUSIC_PREV;
				break;
			case SKILL_LEVEL:
				//Difficulty Select
				m_nDifficulty -= 1;
				if (m_nDifficulty < NORMAL)
				{
					m_nDifficulty = NORMAL;
				}
				break;
			}
		}
		else if (pWii->IsButtonJustPressed(WII_RIGHT) || pDI->KeyPressed(DIK_RIGHT))
		{

			switch(m_nSubSelection)
			{
			case MUSIC_PREV:
				m_nSubSelection = MUSIC_PLAY;
				break;
			case MUSIC_PLAY:
				m_nSubSelection = MUSIC_NEXT;
				break;				
			case SKILL_LEVEL:
				m_nDifficulty += 1;
				if (m_nDifficulty > XTREME)
				{
					m_nDifficulty = XTREME;
				}
				break;
			}

		}

	}
	if ((pWii->IsButtonJustPressed(WII_A) || pDI->KeyPressed(DIK_RETURN)))
	{
		if (!m_bIsSubMenu)
		{
			m_bIsSubMenu = true;
			switch(m_nSelection)
			{
			case MUSIC_SELECTION:
				m_nSubSelection = MUSIC_VOL;
				break;
			case SOUND_SELECTION:
				m_nSubSelection = SOUND_VOL;
				break;
			case DIFFICULTY_SELECTION:
				m_nSubSelection = SKILL_LEVEL;
				break;
			} 
		}
		else
		{
			
			switch(m_nSubSelection)
			{
			case MUSIC_PLAY:
				if (m_bPause)
				{
					CMusicManager::GetInstance()->UnPause();
					m_bPause = false;
				}
				else
				{
					CMusicManager::GetInstance()->Pause();
					m_bPause = true;
				}
				break;
			case MUSIC_NEXT:
				CMusicManager::GetInstance()->NextSong();
				m_bPause = false;
				break;
			case MUSIC_PREV:
				CMusicManager::GetInstance()->PrevSong();
				m_bPause = false;
				break;
			default: 
				m_bIsSubMenu = false;
				break;
			}
		}
	}

	if ((pWii->IsButtonJustPressed(WII_HOME) || pDI->KeyPressed(DIK_ESCAPE)))
	{
		if (m_bIsSubMenu)
		{
			m_bIsSubMenu = false;
		}
		else
		{
			if (!GetFromGame())
			{
				m_bExitingToMain = true; 
			}
			else
			{
				SetFromGame(false);
				CGame::GetInstance()->PopState();
				
			}
		}
	}

	return true;
}

void COptionsState::Update(float fElapsedTime)
{
	m_fVolPanTimer += fElapsedTime;
	if (m_nSelection > 2)
		m_nSelection = 0;
	else if (m_nSelection < 0)
		m_nSelection = 2;



	///Close door then change state///////////////////////////////////////////
	if(m_bExitingToMain)
	{
		CTransitionScreen::GetInstance()->ChangeTo(CMainMenuState::GetInstance());
	}
	//////////////////////////////////////////////////////////////////////////
	
	
}

void COptionsState::Render()
{

	m_cScreen.RenderScreen();


	//////////////////////////////////////////////////////////////////////////
	//Location Setup
	//////////////////////////////////////////////////////////////////////////
	static const int TopX= m_cScreen.GetPosObjMembers("Top",XPOS);
	static const int TopY= m_cScreen.GetPosObjMembers("Top",YPOS);

	static const int LeftX = m_cScreen.GetPosObjMembers("Left",XPOS);
	static const int LeftY = m_cScreen.GetPosObjMembers("Left",YPOS);

	static const int RightX = m_cScreen.GetPosObjMembers("Right",XPOS);
	static const int RightY = m_cScreen.GetPosObjMembers("Right",YPOS);
	
	static const int PrevX = m_cScreen.GetPosObjMembers("PREV",XPOS);
	static const int PrevY = m_cScreen.GetPosObjMembers("PREV",YPOS);

	static const int PlayX = m_cScreen.GetPosObjMembers("PLAY",XPOS);
	static const int PlayY = m_cScreen.GetPosObjMembers("PLAY",YPOS);

	static const int NextX = m_cScreen.GetPosObjMembers("NEXT",XPOS);
	static const int NextY = m_cScreen.GetPosObjMembers("NEXT",YPOS);



	static const int YELLOW = 0xFFFFFF00;
	static const int WHITE  = 0xFFFFFFFF;
	static const int GREEN =  0xFF00FF00;

	static CBitmapFontEngine* pBFE = CBitmapFontEngine::GetInstance();
	static CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();

	static RECT PREV;
	SetRect(&PREV,0,0,PREV.left+32,PREV.top+32);
	static RECT STOP;
	SetRect(&STOP,32,0,STOP.left+32,STOP.top+32);

	static RECT PLAY;
	SetRect(&PLAY,64,0,PLAY.left+32,PLAY.top+32);

	static RECT NEXT;
	SetRect(&NEXT,96,0,NEXT.left+32,NEXT.top+32);


	//////////////////////////////////////////////////////////////////////////

	//Menu Items
	pBFE->RenderString("OPTIONS", m_nFontID, 0.0f, GREEN, 270, TopY+25, 0);
	pBFE->RenderString("MUSIC", m_nFontID, 0.0f, GREEN, LeftX+32, LeftY+25, 0);
	pBFE->RenderString("SOUND", m_nFontID, 0.0f, GREEN, LeftX+32, LeftY+100, 0);
	pBFE->RenderString("Skill", m_nFontID, 0.0f, GREEN, LeftX+32, LeftY+175, 0);


	//Category Menu
	switch(m_nSelection)
	{
	case MUSIC_SELECTION:
		pBFE->RenderString("MUSIC", m_nFontID, 0.0f, YELLOW, LeftX+32, LeftY+25, 0);
		break;
	case SOUND_SELECTION:
		pBFE->RenderString("SOUND", m_nFontID, 0.0f, YELLOW, LeftX+32, LeftY+100, 0);
		break;
	case DIFFICULTY_SELECTION:
		pBFE->RenderString("Skill", m_nFontID, 0.0f, YELLOW, LeftX+32, LeftY+175, 0);
		break;
	}
	

	if (m_bIsSubMenu)
	{
		//Show sub menu options
		switch(m_nSelection)
		{
		case MUSIC_SELECTION:
			pBFE->RenderString("Vol:", m_nFontID, 0.0f, GREEN, RightX, RightY+100, 0); 
			char VolBuf[8];
			sprintf_s(VolBuf, "%i", (int)(CMusicManager::GetInstance()->GetVol()*100));
			pBFE->RenderString(VolBuf, m_nFontID, 0.0f, GREEN, RightX+124, RightY+100, 0);
			
			
			pTM->Draw(m_nMusicButtons,PrevX,PrevY,1,1,&PREV,0,0,0,GREEN);
			pTM->Draw(m_nMusicButtons,NextX,NextY,1,1,&NEXT,0,0,0,GREEN);

			
			if (m_bPause)
			{
				pTM->Draw(m_nMusicButtons,PlayX,PlayY,1,1,&PLAY,0,0,0,GREEN);
			}
			else
			{
				pTM->Draw(m_nMusicButtons,PlayX,PlayY,1,1,&STOP,0,0,0,GREEN);
			}
			break;

		case SOUND_SELECTION:
			pBFE->RenderString("Vol:", m_nFontID, 0.0f, GREEN, RightX, RightY+100, 0);
			char VolSoundBuf[8];
			sprintf_s(VolSoundBuf, "%i", (int)(CSoundManager::GetInstance()->GetVol()));
			pBFE->RenderString(VolSoundBuf, m_nFontID, 0.0f, GREEN, RightX+124, RightY+100, 0);

			pBFE->RenderString("Pan:", m_nFontID, 0.0f, GREEN, RightX, RightY+132, 0);
			char PanBuf[8];
			sprintf_s(PanBuf, "%i", (int)(CSoundManager::GetInstance()->GetPan()));
			pBFE->RenderString(PanBuf, m_nFontID, 0.0f, GREEN, RightX+124, RightY+132, 0);

			break;
		case DIFFICULTY_SELECTION:
			//Skill sub menu
			switch(m_nDifficulty)
			{
			case NORMAL:
				pBFE->RenderString("n00b", m_nFontID, 0.0f, YELLOW, RightX, RightY+100, 0); 
				break;
			case HARD:
				pBFE->RenderString("Pro", m_nFontID, 0.0f, YELLOW, RightX, RightY+100, 0); 
				break;
			case XTREME:		  
				pBFE->RenderString("XTREME!", m_nFontID, 0.0f, YELLOW, RightX, RightY+100, 0); 
				break;
			}
			break;
		}
		

		//Music sub menu
		switch(m_nSubSelection)
		{
		case MUSIC_VOL:
			pBFE->RenderString("Vol:", m_nFontID, 0.0f, YELLOW, RightX, RightY+100, 0);
			char VolBuf[8];
			sprintf_s(VolBuf, "%i", (int)(CMusicManager::GetInstance()->GetVol()*100));
			pBFE->RenderString(VolBuf, m_nFontID, 0.0f, YELLOW, RightX+124, RightY+100, 0);
			break;
		case MUSIC_PLAY:
			if (m_bPause)
			{
				pTM->Draw(m_nMusicButtons,PlayX,PlayY,1,1,&PLAY,0,0,0,YELLOW);
			}
			else
			{	
				pTM->Draw(m_nMusicButtons,PlayX,PlayY,1,1,&STOP,0,0,0,YELLOW);
			}
			break;
		case MUSIC_NEXT:
			pTM->Draw(m_nMusicButtons,NextX,NextY,1,1,&NEXT,0,0,0,YELLOW);
			break;
		case MUSIC_PREV:
			pTM->Draw(m_nMusicButtons,PrevX,PlayY,1,1,&PREV,0,0,0,YELLOW);
			break;
		case SOUND_VOL:

			pBFE->RenderString("Vol:", m_nFontID, 0.0f, YELLOW, RightX, RightY+100, 0);
			char VolSoundBuf[8];
			sprintf_s(VolSoundBuf, "%i", (int)(CSoundManager::GetInstance()->GetVol()));

			pBFE->RenderString(VolSoundBuf, m_nFontID, 0.0f, YELLOW, RightX+124, RightY+100, 0);
			break;
		case SOUND_PAN:
			pBFE->RenderString("Pan:", m_nFontID, 0.0f, YELLOW, RightX, RightY+132, 0);

			char PanBuf[8];
			sprintf_s(PanBuf, "%i", (int)(CSoundManager::GetInstance()->GetPan()));

			pBFE->RenderString(PanBuf, m_nFontID, 0.0f, YELLOW, RightX+124, RightY+132, 0);

			break;
		} 
	}


}
